﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestHCPlayM4
{
    public partial class Form1 : Form
    {
        bool bOpened = false;
        bool bPlayed = false;

        public Form1()
        {
            InitializeComponent();
        }

        public void FileRefDone(int nPort, ushort nUser)
        {
            ushort nSize = 0;
            HCPlayM4.PlayM4_GetRefValue(0, IntPtr.Zero, ref nSize);
            byte[] Datas = new byte[nSize];

            GCHandle hObject = GCHandle.Alloc(Datas, GCHandleType.Pinned);
            IntPtr pObject = hObject.AddrOfPinnedObject();

            HCPlayM4.PlayM4_GetRefValue(0, pObject, ref nSize);

            HCPlayM4.PlayM4_SetRefValue(0, pObject, nSize);

            if (hObject.IsAllocated) hObject.Free();
        }

        private void bPlay_Click(object sender, EventArgs e)
        {
            if (!bOpened)
            {
                HCPlayM4.PlayM4_SetFileRefCallBack(0, FileRefDone, 0);

                HCPlayM4.PlayM4_SetFileEndMsg(0, this.Handle, 0x1111);

                bOpened = HCPlayM4.PlayM4_OpenFile(0, "V:\\test.mp4");
            }

            //if (bOpened && !bPlayed)
                bPlayed = HCPlayM4.PlayM4_Play(0, pictureBox1.Handle);
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x1111)
            {
                MessageBox.Show("播放完毕");
            }
            base.WndProc(ref m);
        }

        private void bStop_Click(object sender, EventArgs e)
        {
            if (bPlayed)
                HCPlayM4.PlayM4_Stop(0);
        }

        private void bFast_Click(object sender, EventArgs e)
        {
            if (bOpened && bPlayed)
                HCPlayM4.PlayM4_Fast(0);
        }
    }
}
